<template>
    <div>
        <basic-container>
            <el-row :gutter="10" class="mb8">
                <el-col :span="1.5">
                    <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
                </el-col>
            </el-row>
            <el-table :data="menuList" style="width: 100%">
                <el-table-column type="expand">
                    <template slot-scope="scope">
                        <div>
                            <div style="font-size: 20px; color: red;margin-left: 40px;"> * 加工路径</div>
                            <el-table :data="scope.row.routes.flat()" style="margin-left: 50px;">
                                <el-table-column prop="sequence" label="加工顺序">
                                </el-table-column>
                                <el-table-column prop="serials" label="加工设备编号" align="center">
                                </el-table-column>
                                <el-table-column prop="name" label="加工设备名称" align="center">
                                </el-table-column>
                            </el-table>
                        </div>
                    </template>
                </el-table-column>
                <!-- <el-table-column label="机种名称" prop="aircraft">
                </el-table-column>
                <el-table-column label="产品配方" prop="formula" align="center">
                </el-table-column> -->
                <el-table-column label="路线名称" prop="name" align="center">
                </el-table-column>
                <!-- <el-table-column label="添加工艺用户" prop="user_name" align="center">
                </el-table-column> -->
                <el-table-column label="添加时间" prop="time" align="center">
                </el-table-column>
                <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                    <template slot-scope="scope">

                        <el-button size="mini" type="text" icon="el-icon-edit"
                            @click="handleUpdate(scope.row)">修改</el-button>
                        <el-button size="mini" type="text" icon="el-icon-delete"
                            @click="handleDelete(scope.row, 1)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body :close-on-click-modal="false">
                <el-form ref="form" :model="form" :rules="rules" label-width="120px">
                    <el-row>

                        <el-col :span="19">
                            <el-form-item label="工艺路线名称" prop="name">
                                <el-input v-model="form.name" placeholder="请输入工艺路线名称" />
                            </el-form-item>

                        </el-col>
                        <el-col :span="19" v-if="title =='工艺路径名称' ">
                            <el-form-item label="机种名称" prop="aircraft_id">
                                <el-select v-model="form.aircraft_id" placeholder="请选择">
                                    <el-option v-for="item in reworkList" :key="item.key" :label="item.value"
                                        :value="item.key">
                                    </el-option>
                                </el-select>
                            </el-form-item>

                        </el-col>

                        <el-col :span="19">
                            <el-form-item label="加工工艺路线" prop="routes">
                                <el-table :data="dataList" style="width: 100%">
                                    <el-table-column prop="sequence" label="加工顺序">
                                    </el-table-column>
                                    <el-table-column prop="name" label="加工设备名称">
                                    </el-table-column>
                                    <el-table-column prop="serials" label="加工设备编号">
                                    </el-table-column>
                                    <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                                        <template slot-scope="scope">
                                            <el-button size="mini" type="text" icon="el-icon-delete"
                                                @click="handleDatalist(scope.$index)">
                                                删除</el-button>
                                        </template>
                                    </el-table-column>
                                </el-table>
                            </el-form-item>
                            <div style="display: flex;margin-left: 15%;">
                                <div>
                                    <h3>加工顺序</h3>
                                    <el-select v-model="value" placeholder="请选择">
                                        <el-option v-for="item in sequenceOptions" :key="item.value" :label="item.label"
                                            :value="item.value">
                                        </el-option>
                                    </el-select>
                                </div>
                                <div style="margin: 0 20px;">
                                    <h3>设备名称</h3>
                                    <el-select v-model="valueName" placeholder="请选择">
                                        <el-option v-for="item in optionrole " :key="item.facility_id"
                                            :label="'(' + item.serials + ')' + item.name" :value="item.facility_id">
                                        </el-option>
                                    </el-select>
                                    <el-button type="primary" style="margin-left: 30px;"
                                        :disabled="value == '' || valueName == ''" @click="addprocess">添
                                        加</el-button>
                                </div>
                            </div>
                        </el-col>
                    </el-row>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button type="primary" @click="submitForm">确 定</el-button>
                    <el-button @click="cancel">取 消</el-button>
                </div>
            </el-dialog>
            <pagination v-show="total > 0" :total="total" :page.sync="queryParams.index" :limit.sync="queryParams.size"
                @pagination="getList" />
        </basic-container>
    </div>
</template>
<script>
import { getrouteslist, grtformula, roleStation, addroutes, eidroutes, dleroutes, addstart, addstop } from "@/api/template";
import {
 
    grtAircraft,
   

    
} from "@/api/tasks";
import { mapGetters } from "vuex";
export default {
    data() {
        return {
            tabPosition: 'left',
            reworkList: [],
            loading: false,
            valueName: '',
            total: 0,
            queryParams: {
                index: 1,
                size: 10,
            },
            sequenceOptions: [{
                value: '1',
                label: '1'
            }, {
                value: '2',
                label: '2'
            }, {
                value: '3',
                label: '3'
            }, {
                value: '4',
                label: '4'
            }, {
                value: '5',
                label: '5'
            },
            {
                value: '6',
                label: '6'
            },
            {
                value: '7',
                label: '7'
            },
            {
                value: '8',
                label: '8'
            },
            {
                value: '9',
                label: '9'
            },
            {
                value: '10',
                label: '10'
            },
            {
                value: '11',
                label: '11'
            },
            {
                value: '12',
                label: '12'
            },
            {
                value: '13',
                label: '13'
            },
            {
                value: '14',
                label: '14'
            },
            {
                value: '15',
                label: '15'
            },
            {
                value: '16',
                label: '16'
            },
            {
                value: '17',
                label: '17'
            },
            {
                value: '18',
                label: '18'
            },
            {
                value: '19',
                label: '19'
            },
            {
                value: '20',
                label: '20'
            },
            {
                value: '21',
                label: '21'
            },
            {
                value: '22',
                label: '22'
            },
            {
                value: '23',
                label: '23'
            },
            {
                value: '24',
                label: '24'
            },
            {
                value: '25',
                label: '25'
            },
            {
                value: '26',
                label: '26'
            },
            {
                value: '27',
                label: '27'
            },
            {
                value: '28',
                label: '28'
            },
            {
                value: '29',
                label: '29'
            },
            {
                value: '30',
                label: '30'
            }
            ],
            optionname: '',
            optionserials: '',
            optionId: '',
            optionrole: [],
            option: [],
            open: false,
            title: "",
            dataList: [],
            value: '',
            form: {},
            menuList: [],
            rules: {
                aircraft: [
                    { required: true, message: "机种名称不能为空", trigger: "blur" }
                ],
                name: [
                    { required: true, message: "工艺名称不能为空", trigger: "blur" }
                ],
                formula_id: [
                    { required: true, message: "工艺配方不能为空", trigger: "blur" }
                ],
                aircraft_id:[
                { required: true, message: "机种不能为空", trigger: "blur" }
                ]
            }
        }
    },
    created() {
        this.getList()

        this.getroleStation()
    },
    beforeDestroy() {

    },
    computed: {
        ...mapGetters(["userInfo", "menu"]),
    },
    methods: {

        /** 查询菜单列表 */
        getList() {
            this.loading = true;
            getrouteslist(this.queryParams).then(response => {
                console.log(response, "0000000000000000")
                this.menuList = response.data.data
                this.total = response.data.total;
                this.loading = false;
            })
        },
        // 获取配方
        grtformulaList() {
            grtformula().then(res => {
                this.option = res.data.data
            })
        },
        // 获取pLC设备
        getroleStation() {
            roleStation().then(res => {
                this.optionrole = res.data.data
            })
        },

        getcourseId() {
            grtAircraft().then(res => {
                console.log(res);
                this.reworkList = res.data.data
            })
        },
        // 表单重置
        reset() {
            this.form = {
                routes: [],
                name: null,
                formula_id: null,
                aircraft: null,
                aircraft_id: null,
                serials: null,
                start: null,
                stop: null,
                route_id: null,
            };
        },
        handleUpdate(row) {
            this.reset()
            this.form = row
            this.dataList = row.routes.flat()
            this.open = true;
            this.title = "修改工艺路径名称";
        },
        handlstop() {
            addstop().then(res => {
                console.log(res);
                this.$message({
                    message: "成功",
                    type: "success",
                });
            })

        },
        handlstart() {
            addstart().then(res => {
                console.log(res);
                this.$message({
                    message: "成功",
                    type: "success",
                });
            })

        },
        handleDelete(row) {
            console.log(row)
            let messsId = {
                template_id: row.template_id
            }
            this.$confirm('是否确认删除名称为"' + row.name + '"的工艺路径？')
                .then(() => {
                    dleroutes(messsId).then(() => {
                        this.$message({
                            message: "删除成功",
                            type: "success",
                        });
                        this.open = false;
                        this.getList();
                        this.reset();
                    });
                })
                .catch(() => { });
        },
        cancel() {
            this.open = false;
            this.getList();
            this.reset();
        },
        handleAdd() {
            this.open = true;
            this.reset();
            this.dataList = []
            this.value = ''
            this.valueName = ''
            this.title = "工艺路径名称";
            this.getcourseId()
        },
        addprocess() {
            for (var i = 0; i < this.optionrole.length; i++) {
                if (this.valueName == this.optionrole[i].facility_id) {
                    this.optionserials = this.optionrole[i].serials
                    this.optionname = this.optionrole[i].name
                    this.optionId = this.optionrole[i].facility_id
                }
            }

            for (var j = 0; j < this.dataList.length; j++) {
                if (this.dataList[j].sequence == this.value && this.dataList[j].facility_id == this.optionId) {
                    return this.$message.error('已经有相同步骤了');
                }
            }
            this.dataList.push({
                name: this.optionname,
                serials: this.optionserials,
                sequence: this.value,
                facility_id: this.optionId
            })
            this.value = ''
            this.valueName = ''
            this.dataList.sort(function (a, b) {
                return parseInt(a.sequence) - parseInt(b.sequence);
            })
        },
        handleDatalist(index) {
            this.dataList.splice(index, 1);
        },
        /** 提交按钮 */
        submitForm() {
            console.log(this.form);
           
            this.$refs["form"].validate((valid) => {
                if (valid) {
                    if (this.dataList.length == 0) return this.$message.error('加工工艺路径不能为空')
                    let sequenceMap = {};
                    for (var i = 0; i < this.dataList.length; i++) {
                        let currentSequence = this.dataList[i].sequence;
                        let currentFacilityId = this.dataList[i].facility_id;
                        if (!sequenceMap[currentSequence]) {
                            sequenceMap[currentSequence] = { ft_ids: [] };
                        }
                        sequenceMap[currentSequence].ft_ids.push(currentFacilityId);
                    }
                    this.form.routes = Object.values(sequenceMap);
                    if (this.title == "工艺路径名称") {
                        addroutes(this.form).then(() => {
                            this.$message({
                                message: "添加成功",
                                type: "success",
                            });
                            this.open = false;
                            this.getList();
                            this.reset();
                        });
                    } else {
                        eidroutes(this.form).then(() => {
                            this.$message({
                                message: "修改成功",
                                type: "success",
                            });
                            this.open = false;
                            this.getList();
                            this.reset();
                        });
                    }
                }
            });
        },
    }
};
</script>
<style lang="scss">
.demo-table-expand {
    font-size: 0;

}

.demo-table-expand label {
    width: 90px;
    color: #99a9bf;
}

.demo-table-expand .el-form-item {
    margin-right: 0;
    margin-bottom: 0;
    width: 50%;
}

.chengHigh {
    .el-tabs__item {
        height: 60px !important;
        line-height: 60px !important;
        font-size: 16px !important;
        padding: 0 40px !important;
    }

}

.SecondaryContent {
    width: 100%;
    display: flex;

    .ContentItem {
        flex: 1;
    }
}

.Contentarea {
    display: flex;


}

.using {
    font-size: 16px;
    padding: 10px 0;
    width: 50%;

    text-align: center;

}

.parallel {
    font-size: 16px;
    padding: 10px 0;
    width: 50%;

}

.table {
    display: flex;
    justify-content: space-around;
    font-size: 14px;
    line-height: 40px;
    font-weight: 600;
    height: 45px;
    background-color: #f8f8f9;
    color: #515a6e;
    border-bottom: 1px solid #EBEEF5;
}
</style>
